﻿using System.Collections;
using System.Collections.Generic;
using UnityEngine;

public class TestCharactorMove : MonoBehaviour
{
    CharacterController cc;
    public float gravity = 1.0f;
    public float speed = 1.0f;
    public float jumpSpeed = 50.0f;

    private Vector3 moveDirection = Vector3.zero;
    // Use this for initialization
    void Start()
    {
        cc = GetComponent<CharacterController>();

    }

    // Update is called once per frame
    void Update()
    {
        if (cc.isGrounded)
        {
            moveDirection = new Vector3(0, 0, 0);
            moveDirection = transform.TransformDirection(moveDirection);

            if (Input.GetKey(KeyCode.D))
                moveDirection.x += speed;
            if (Input.GetKey(KeyCode.A))
                moveDirection.x -= speed;
            if (Input.GetKey(KeyCode.Space))
                moveDirection.y = jumpSpeed; 
        }
        else
        {
            //先落地
            moveDirection.y -= gravity * Time.deltaTime;	
        }
        cc.Move(moveDirection * Time.deltaTime);
    }
}
